// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CertificateAction.java

package com.iphaeton.lms.action.certificate;

import com.iphaeton.common.helper.*;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.certificate.ICertificateService;
import com.iphaeton.lms.business.exam.IQuestionService;
import com.iphaeton.lms.business.organize.IGroupService;
import com.iphaeton.lms.business.organize.IOrganizeService;
import com.iphaeton.lms.pojo.certificate.Certificate;
import com.iphaeton.lms.pojo.exam.Paper;
import com.iphaeton.lms.pojo.organize.Organize;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import org.dom4j.io.XMLWriter;

// Referenced classes of package com.iphaeton.lms.action.certificate:
//            CertificateBizSupportAction

public class CertificateAction extends CertificateBizSupportAction
{

    public CertificateAction()
    {
        cal = Calendar.getInstance();
        time = cal.getTime();
        df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        currentTime = df.format(time);
    }

    public ActionForward createCertificate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String name = (String)daf.get("name");
        String comment = (String)daf.get("comment");
        Integer date = (Integer)daf.get("date");
        Integer type = (Integer)daf.get("type");
        FormFile certification = (FormFile)daf.get("certification");
        String map = "";
        java.util.List list = null;
        Organize org = null;
        String str = "";
        Certificate cert = new Certificate();
        cert.setName(name);
        cert.setComment(comment);
        cert.setEffective(date);
        cert.setType(type);
        cert.setCertificateDate(currentTime);
        cert.setState(new Integer(2));
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String user_id = userSession.getId();
            String organize_id = userSession.getManageOrgId();
            cert.setCreater(user_id);
            cert.setOrganizeId(organize_id);
            certificate.save(cert);
            if(type.equals(new Integer(1)))
            {
                map = "organize_assign";
                org = organize.findOrganizeById(organize_id);
                str = organize.getOrgCkBTree(org, "tree");
                request.setAttribute("str", str);
            } else
            if(type.equals(new Integer(2)))
            {
                map = "station_assign";
                list = organize.findGroup(organize.findOrganizeById(organize_id), 'g');
            } else
            if(type.equals(new Integer(3)))
            {
                map = "duty_assign";
                list = organize.findGroup(organize.findOrganizeById(organize_id), 'z');
            }
            if(certification != null && !certification.getFileName().equals(""))
            {
                upload.upload(certification, request.getRealPath("/") + "upload" + File.separator + "certificate" + File.separator + cert.getId() + File.separator);
                cert.setCertification(certification.getFileName());
                certificate.save(cert);
            }
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("certificate", cert);
        request.setAttribute("list", list);
        return mapping.findForward(map);
    }

    public ActionForward removeCertificate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id[] = request.getParameter("id").split(";");
        try
        {
            for(int i = 0; i < id.length; i++)
                certificate.remove(id[i]);

        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("manageCertificate");
    }

    public ActionForward setCertificateGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("groupList");
        String group_id[] = id.split("&");
        Certificate cert = null;
        try
        {
            cert = (Certificate)request.getSession().getAttribute("certificate");
            request.getSession().removeAttribute("certificate");
            cert = certificate.save(cert);
            for(int i = 0; i < group_id.length; i++)
                certificate.setCertificateGroup(cert.getId(), group_id[i]);

            cert = certificate.findCertificateById(cert.getId());
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("certificate", cert);
        return mapping.findForward("certificate_ok");
    }

    public ActionForward setCertificateOrganize(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String orgSelect = request.getParameter("organizeSel");
        String organize_id[] = orgSelect.split("&");
        Certificate cert = null;
        try
        {
            cert = (Certificate)request.getSession().getAttribute("certificate");
            request.getSession().removeAttribute("certificate");
            cert = certificate.save(cert);
            for(int i = 0; i < organize_id.length; i++)
                certificate.setCertificateOrganize(cert.getId(), organize_id[i]);

            cert = certificate.findCertificateById(cert.getId());
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("certificate", cert);
        return mapping.findForward("certificate_ok");
    }

    public ActionForward manageCertificate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        int pageSize = 10;
        int currentPage = 1;
        String row = request.getParameter("row");
        String strPage = request.getParameter("page");
        String strState = request.getParameter("state");
        int state = 0;
        if(strState != null)
            state = Integer.parseInt(strState);
        java.util.List list = null;
        try
        {
            String organize_id = ((UserSession)request.getSession().getAttribute("userSession")).getManageOrgId();
            if(strPage != null && !strPage.trim().equals(""))
                currentPage = Integer.parseInt(strPage);
            pageInfo = certificate.queryCertificate(organize_id, state, row, pageSize);
            pageInfo.setPage(currentPage);
            pageInfo.setRow(row);
            pageInfo.setState(state);
            list = certificate.findPageList(pageInfo);
            request.getSession().setAttribute("pageInfo", pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("state", strState);
        request.setAttribute("list", list);
        return mapping.findForward("certificate_manage");
    }

    public ActionForward selectCertificate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String row = request.getParameter("row");
        java.util.List list = null;
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String user_id = userSession.getId();
            String org_id = userSession.getManageOrgId();
            String org_url = organize.findOrganizeById(org_id).getUrl();
            list = certificate.currentCertificate(user_id, org_id, org_url, row);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("current_time", currentTime.toString());
        request.setAttribute("list", list);
        return mapping.findForward("certificate_list");
    }

    public ActionForward myCertificate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String row = request.getParameter("row");
        java.util.List list = null;
        try
        {
            String user_id = ((UserSession)request.getSession().getAttribute("userSession")).getId();
            list = certificate.myCertificate(user_id, row);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("list", list);
        return mapping.findForward("my_certificate");
    }

    public ActionForward certificateExam(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        Certificate cert = null;
        try
        {
            cert = certificate.findCertificateById(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("certificate", cert);
        return mapping.findForward("certificate_exam");
    }

    public ActionForward queryQuestionForPaper(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        Paper p = new Paper();
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String organize_id = userSession.getManageOrgId();
            String user_id = userSession.getId();
            Integer exam_access = new Integer(request.getParameter("exam_access"));
            Integer period = new Integer(request.getParameter("exam_time"));
            Integer condition = new Integer(request.getParameter("condition"));
            String name = request.getParameter("exam_name");
            String start_time = request.getParameter("start_time");
            String end_time = request.getParameter("end_time");
            String start_hour = request.getParameter("start_hour");
            String start_minute = request.getParameter("start_minute");
            String end_hour = request.getParameter("end_hour");
            String end_minute = request.getParameter("end_minute");
            p.setName(name);
            p.setType(exam_access);
            p.setSort(new Integer(5));
            p.setPeriod(period);
            p.setDeleted(new Integer(1));
            p.setOrganizeId(organize_id);
            p.setCreater(user_id);
            p.setScore(new Integer(0));
            p.setState(new Integer(2));
            p.setStartTime(start_time + " " + start_hour + ":" + start_minute);
            p.setEndTime(end_time + " " + end_hour + ":" + end_minute);
            p = question.createPaper(p);
            Certificate cert = certificate.findCertificateById(id);
            cert.setCondition(condition);
            cert.setState(new Integer(1));
            cert.setPaperId(p.getId());
            certificate.save(cert);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("id", id);
        request.getSession().setAttribute("paper", p);
        return mapping.findForward("makePaper");
    }

    public ActionForward queryPaperQuestionPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        java.util.List sortList = new LinkedList();
        java.util.List hardList = new LinkedList();
        java.util.List pointList = new LinkedList();
        java.util.List stationList = new LinkedList();
        java.util.List dutyList = new LinkedList();
        try
        {
            sortList = question.queryQuestionSort();
            hardList = question.queryQuestionHard();
            stationList = group.getGroupList("g", "");
            dutyList = group.getGroupList("z", "");
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            return mapping.findForward("failure");
        }
        request.setAttribute("sortList", sortList);
        request.setAttribute("hardList", hardList);
        request.setAttribute("pointList", pointList);
        request.setAttribute("stationList", stationList);
        request.setAttribute("dutyList", dutyList);
        return mapping.findForward("paper_question_condition");
    }

    public ActionForward certificateInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        java.util.List list = null;
        String reStr = "";
        String id = request.getParameter("id");
        String state = request.getParameter("state");
        if(state.equals("1"))
            reStr = "certificate_info";
        else
        if(state.equals("2"))
            reStr = "certificate_failure";
        try
        {
            list = certificate.certificateInfo(id, Integer.parseInt(state));
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("list", list);
        return mapping.findForward(reStr);
    }

    public ActionForward certificateAgain(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        String user_id = request.getParameter("userId");
        java.util.List list;
        try
        {
            String paper_id = certificate.findCertificateById(id).getPaperId();
            certificate.certificateAgain(id, user_id, paper_id);
            list = certificate.certificateInfo(id, 2);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("list", list);
        return mapping.findForward("certificate_failure");
    }

    public ActionForward certificateTree(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0L);
        String state = request.getParameter("state");
        org.dom4j.Document doc = null;
        try
        {
            String id = ((UserSession)request.getSession().getAttribute("userSession")).getManageOrgId();
            String url = organize.findOrganizeById(id).getUrl();
            doc = certificate.getTree(id, url, state);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
        response.setContentType("text/xml; charset=UTF-8");
        javax.servlet.ServletOutputStream sos = response.getOutputStream();
        XMLWriter writer = new XMLWriter(sos);
        writer.write(doc);
        writer.flush();
        writer.close();
        return null;
    }

    public ActionForward certificateDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        Certificate cert;
        try
        {
            cert = certificate.findCertificateById(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("certificate", cert);
        return mapping.findForward("certificate_detail");
    }

    public ActionForward showCertificate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        Certificate cert;
        try
        {
            cert = certificate.findCertificateById(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("certificate", cert);
        request.setAttribute("current_time", currentTime.toString());
        return mapping.findForward("show_certificate");
    }

    private static Logger logger;
    private DynaActionForm daf;
    private Pagination pageInfo;
    Calendar cal;
    Date time;
    DateFormat df;
    String currentTime;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.action.certificate.CertificateAction.class.getName());
    }
}
